package com.wuchao.signer.proxy.cglibs;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author wuchao66
 * @Date 2023/10/20 11:27
 * @PackageName:com.wuchao.signer.proxy.cglibs
 * @ClassName: ProxyFactory
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class ProxyFactory implements MethodInterceptor {
    private     UserDao userDao;//目标对象
    public ProxyFactory(UserDao userDao){
        this.userDao=userDao;
    }
    //给目标对象创建一个代理对象
    public Object getProxyInstance(){
        //1.工具类
        Enhancer en = new Enhancer();
        //2.设置父类
        en.setSuperclass(userDao.getClass());
        //3.设置回调函数
        en.setCallback(this);
        //4.创建子类(代理对象)
        return en.create();

    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("开1");

        Object invoke1 = method.invoke(userDao, objects);
        System.out.println("关1");

        return invoke1;
    }
}
